<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-form :inline="true">
        <el-form-item>
          <el-input class="search" v-model="page.search" placeholder="请输入商品名称" />
          <el-button :icon="Search" @click="search"></el-button>
        </el-form-item>
        <!-- 后面的按钮 -->
        <el-form-item>
          <el-button type="primary" @click="goodAdd">添加商品</el-button>
        </el-form-item>
      </el-form>
      <!-- 表格 stripe是否显示斑马 data数组,展示的数据,border 是否有边框  -->
      <el-table stripe :data="goodsListData" border style="width: 100%">
        <!-- width 某一列的宽,label 表格头的名字  prop 里边数对象的属性名 type="index" 是否使用序列号  -->
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量(kg)" width="90"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="120"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="small" :icon="EditPen" />
            <el-button type="danger" size="small" :icon="Delete" @click="delGoodsById(scope.row.goods_id)" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 
        v-model:current-page当前页数  v-model:page-size每页显示条目个数
        small是否使用小型分页样式  disabled是否禁用分页
      -->
      <el-pagination
        v-model:current-page="page.currentPage"
        v-model:page-size="page.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        small
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
// 操作字体 图标
import { Delete, EditPen, Search } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/util/request'
// 跳转
import { useRouter } from 'vue-router'
// 列表数据
const goodsListData = ref([])
// const users = []
// 分页
// 总条数
const total = ref(100)
const page = ref({
  // 搜索框
  search: '',
  // 当前页
  currentPage: 1,
  // 每页查询条数
  pageSize: 10,
})
// 是否禁用分页
const disabled = ref(false)
// 是否为分页按钮添加背景色
const background = ref(false)
// page-size 改变时触发
const handleSizeChange = () => {
  getGoodes()
  console.log('handleSizeChange')
}
// current-page 改变时触发
const handleCurrentChange = () => {
  getGoodes()
  console.log('handleCurrentChange')
}
// 添加商品跳转
const router = useRouter()
const goodAdd = () => {
  router.push('home/goods/add')
}

// 分页获取列表数据
const getGoodes = async () => {
  const { data, meta }: any = await request({
    // url: `/goods?&pagenum=${this.$data.pagenum}&pagesize=${this.$data.pagesize}`,
    url: '/goods',
    methods: 'GET',
    params: {
      query: page.value.search,
      pagenum: page.value.currentPage,
      pagesize: page.value.pageSize,
    },
    // params: page.value,
  })
  if (meta.status !== 200) {
    // this.$message.error(meta.msg)
    ElMessage.error('获取数据失败')
    return
  }
  goodsListData.value = data.goods
  total.value = data.total
}
onMounted(() => {
  getGoodes()
})
// input搜索按钮
const search = () => {
  // 每次搜索,页数从1开始
  page.value.currentPage = 1
  getGoodes()
}
// 删除数据
const delGoodsById = async (id: any) => {
  try {
    await ElMessageBox.confirm('确定删除吗，是否继续？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    // 往后台数据
    const { meta }: any = await request({
      url: `/goods/${id}`,
      method: 'delete',
    })
    if (meta.status !== 200) {
      return ElMessage.error('删除失败')
    }
    // 删除成功 重新获取数据 刷新页面
    getGoodes()
    ElMessage.success('删除成功')
  } catch (error) {
    return ElMessage.info('取消删除')
  }
}
</script>

<style scoped>
.el-breadcrumb {
  margin-top: 10px;
}

.el-card {
  margin-top: 20px;
}

.search {
  width: 540px;
}
</style>
